package io.renren.modules.dictionary.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.common.annotation.SysLog;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.dictionary.service.DictionaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.dictionary.entity.DictypesEntity;
import io.renren.modules.dictionary.service.DictypesService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-10-17 14:15:28
 */
@RestController
@RequestMapping("/dictionary/dictypes")
public class DictypesController {
	@Autowired
	private DictypesService dictypesService;
	@Autowired
	private DictionaryService dictionaryService;
	/**
	 * 只读
	 */
	private final Integer READONLY = 1;
	
	/**
	 * 列表
	 */
	@SysLog("查询字典类型列表")
	@RequestMapping("/list")
	@RequiresPermissions("dictionary:dictypes:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<DictypesEntity> dictypesList = dictypesService.queryList(query);
		int total = dictypesService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(dictypesList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@SysLog("查询字典类型信息")
	@RequestMapping("/info/{dictypeid}")
	@RequiresPermissions("dictionary:dictypes:info")
	public R info(@PathVariable("dictypeid") Integer dictypeid){
		DictypesEntity dictypes = dictypesService.queryObject(dictypeid);
		
		return R.ok().put("dictypes", dictypes);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存字典类型")
	@RequestMapping("/save")
	@RequiresPermissions("dictionary:dictypes:save")
	public R save(@RequestBody DictypesEntity dictypes){
		ValidatorUtils.validateEntity(dictypes, AddGroup.class);
		if (dictypes.getIsreadonly() != null) {
			return R.error("只读属性不可设置");
		}
		dictypes.setCreatetime(new Date());
		dictypes.setUpdatetime(new Date());
		dictypesService.save(dictypes);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改字典类型")
	@RequestMapping("/update")
	@RequiresPermissions("dictionary:dictypes:update")
	public R update(@RequestBody DictypesEntity dictypes){
		ValidatorUtils.validateEntity(dictypes, UpdateGroup.class);
		//判断只读属性是否修改
		DictypesEntity dictypesEntity = dictypesService.queryObject(dictypes.getDictypeid());
		if (READONLY.equals(dictypesEntity.getIsreadonly())) {
			return R.error("只读属性不可修改");
		}
		dictypes.setUpdatetime(new Date());
		dictypesService.update(dictypes);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除字典类型")
	@RequestMapping("/delete")
	@RequiresPermissions("dictionary:dictypes:delete")
	public R delete(@RequestBody Integer[] dictypeids){
		//删除之前判断是否有只读字段
		for (int i = 0; i < dictypeids.length;i++){
			DictypesEntity entity = dictypesService.queryObject(dictypeids[i]);
			if (READONLY.equals(entity.getIsreadonly())){
				return R.error("只读字段不可删除");
			}
		}
		//删除字典类型前删除相应数据字典
		for (int i = 0; i < dictypeids.length;i++){
			DictypesEntity entity = dictypesService.queryObject(dictypeids[i]);
			dictionaryService.deleteBatchByDicTypeId(dictypeids[i]);
		}
		dictypesService.deleteBatch(dictypeids);
		
		return R.ok();
	}

	@SysLog("查询字典类型列表（不分页）")
	@RequestMapping("/queryTypeNames")
	@RequiresPermissions("dictionary:dictypes:list")
	public R queryTypeNames(){
		//查询类型数据

		List<DictypesEntity> dictypesList = dictypesService.queryTypeNames();


		return R.ok().put("data", dictypesList);
	}
	
}
